define(["jquery", "bootstrap", "backend", "table", "form"], function (
  $,
  undefined,
  Backend,
  Table,
  Form
) {
  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "subject/subject/index",
          add_url: "subject/subject/add",
          edit_url: "subject/subject/edit",
          del_url: "subject/subject/del",
          multi_url: "subject/subject/multi",
          table: "subject",
        },
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "user.id",
        columns: [
          [
            { checkbox: true },
            { field: "id", title: __("Id"), sortable: true },
            { field: "group.name", title: __("Group") },
            { field: "username", title: __("Username"), operate: "LIKE" },
            { field: "nickname", title: __("Nickname"), operate: "LIKE" },
            { field: "email", title: __("Email"), operate: "LIKE" },
            { field: "mobile", title: __("Mobile"), operate: "LIKE" },
            {
              field: "avatar",
              title: __("Avatar"),
              events: Table.api.events.image,
              formatter: Table.api.formatter.image,
              operate: false,
            },
            {
              field: "level",
              title: __("Level"),
              operate: "BETWEEN",
              sortable: true,
            },
            {
              field: "gender",
              title: __("Gender"),
              visible: false,
              searchList: { 1: __("Male"), 0: __("Female") },
            },
            {
              field: "score",
              title: __("Score"),
              operate: "BETWEEN",
              sortable: true,
            },
            {
              field: "successions",
              title: __("Successions"),
              visible: false,
              operate: "BETWEEN",
              sortable: true,
            },
            {
              field: "maxsuccessions",
              title: __("Maxsuccessions"),
              visible: false,
              operate: "BETWEEN",
              sortable: true,
            },
            {
              field: "logintime",
              title: __("Logintime"),
              formatter: Table.api.formatter.datetime,
              operate: "RANGE",
              addclass: "datetimerange",
              sortable: true,
            },
            {
              field: "loginip",
              title: __("Loginip"),
              formatter: Table.api.formatter.search,
            },
            {
              field: "jointime",
              title: __("Jointime"),
              formatter: Table.api.formatter.datetime,
              operate: "RANGE",
              addclass: "datetimerange",
              sortable: true,
            },
            {
              field: "joinip",
              title: __("Joinip"),
              formatter: Table.api.formatter.search,
            },
            {
              field: "status",
              title: __("Status"),
              formatter: Table.api.formatter.status,
              searchList: { normal: __("Normal"), hidden: __("Hidden") },
            },
            {
              field: "operate",
              title: __("Operate"),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate,
            },
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },
    add: function () {
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    recyclebin: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          recyclebin_url: "subject/subject/recyclebin",
          restore_url: "subject/subject/restore",
          del_url: "subject/subject/destroy",
          table: "subject",
        },
      }); // 获取到table标签的id

      var table = $("#table");
      table.bootstrapTable({
        // 当访问列表发起ajax请求数据
        url: $.fn.bootstrapTable.defaults.extend.recyclebin_url, // 主键
        pk: "id", // 设置默认排序
        sortName: "createtime",
        columns: [
          [
            { checkbox: true },
            { field: "id", title: __("Id") },
            {
              field: "thumbs",
              title: __("Thumbs"),
              events: Table.api.events.image,
              formatter: Table.api.formatter.image,
            },
            { field: "title", title: __("Title") },
            { field: "like_count", title: __("Likes") },
            { field: "price", title: __("Price") },
            { field: "cate.name", title: __("Cateid") },
            {
              field: "createtime",
              title: __("Createtime"),
              addclass: "datetimerange",
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "deletetime",
              title: __("Deletetime"),
              addclass: "datetimerange",
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "operate",
              title: __("Operate"),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate,
              buttons: [
                {
                  name: "restore",
                  title: "还原数据",
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  icon: "fa fa-reply",
                  url: $.fn.bootstrapTable.defaults.extend.restore_url,
                  confirm: "确认还原该条数据？",
                  success: function () {
                    // 操作完成后是否刷新列表
                    table.bootstrapTable("refresh");
                  },
                },
              ],
            },
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);

      $(document).on("click", ".btn-restore", function () {
        let ids = Table.api.selectedids(table);

        Layer.confirm("是否确认还原课程？", function (index) {
          Fast.api.ajax(
            {
              url: $.fn.bootstrapTable.defaults.extend.restore_url,
              data: {
                ids,
              },
            },
            function () {
              Layer.close(index); // 刷新表格

              table.bootstrapTable("refresh");
            }
          );
        });
      });
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };
  return Controller;
});
